package com.example.kafka_demo.config;

import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.TopicBuilder;

/**
 * @author Keson
 * @version 1.0
 * @description: TODO kafka配置类
 * @date 2021/9/6 15:33
 */
@Configuration
@EnableKafka //开启Kafka监听器标注的端点
public class KafkaConfig {

    @Value("${kafka.topic.kafka-demo-topic}")
    private String orderTopic;

    @Bean
    //条件匹配，如果配置kafka.topic.autoCreate=true的话，该配置类生效，反之则无效
    @ConditionalOnProperty(name = "kafka.topic.autoCreate", havingValue = "true")
    public NewTopic orderTopic(){
        return TopicBuilder.name(orderTopic).build();
    }

}
